package br.com.dufs.mvc.controller;

import java.awt.event.ActionEvent;
import java.util.List;

import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import br.com.dufs.mvc.model.Terras;
import br.com.dufs.mvc.service.TerrasBOService;

@Controller
@Scope("view")
public class TerrasMB {

	private Terras terras;
	private DataModel<Terras> listaTerras;

	@Autowired
	TerrasBOService terrasBOService;

	public DataModel<Terras> getListar() {
		List<Terras> lista = terrasBOService.todos();

		listaTerras = new ListDataModel<Terras>(lista);
		return listaTerras;
	}

	public Terras getTerras() {
		return terras;
	}

	public void setTerras(Terras data) {
		this.terras = data;
	}

	public void prepararAdicionar(ActionEvent actionEvent) {
		this.terras = new Terras();
	}

	public void prepararAlterar(ActionEvent actionEvent) {
		this.terras = (Terras) (this.listaTerras.getRowData());
	}

	public void excluir() {

		Terras terrasTemp = (Terras) (this.listaTerras.getRowData());
		terrasBOService.excluir(terrasTemp);

	}

	public void adicionar(ActionEvent actionEvent) {

		terrasBOService.salvar(terras);

	}

	public void alterar(ActionEvent actionEvent) {

		terrasBOService.atualizar(terras);

	}

}
